<template>
    <el-form ref="form" label-width="80px" style="padding:20px" size="mini" label-position="left">
        
        <el-form-item label="产品名称">
            <el-input v-model="goods.name"></el-input>
        </el-form-item>

        <el-form-item label="产品编号">
            <el-input v-model="goods.number"></el-input>
        </el-form-item>

        <el-form-item label="标准价格">
            <el-input v-model="goods.price"></el-input>
        </el-form-item>

        <el-form-item label="商品库存" v-show="!goods.id">
            <el-input v-model="goods.nums"></el-input>
        </el-form-item>

        <el-form-item label="产品描述">
            <el-input type="textarea" v-model="goods.remarks"></el-input>
        </el-form-item>

        <el-form-item label="所属分类">
            <el-input
				v-model="cname"
				disabled>
				<el-button slot="append" @click="selcate" icon="el-icon-h-search"></el-button>
			</el-input>
        </el-form-item>

        <el-form-item label="所属品牌">
            <el-input
				v-model="bname"
				disabled>
				<el-button slot="append" @click="selbran" icon="el-icon-h-search"></el-button>
			</el-input>
        </el-form-item>

        <el-form-item label="销售提成">
			<el-radio-group v-model="goods.tdtype">
				<el-radio :label="1">百分比</el-radio>
				<el-radio :label="2">固定金额</el-radio>
			</el-radio-group>
			<el-input v-model="goods.percent" placeholder="请输入销售百分比提成(0 ~ 100)" v-if="goods.tdtype==1"></el-input>
			<el-input v-model="goods.fixed" placeholder="请输入销售固定金额提成" v-else></el-input>
		</el-form-item>
		<el-form-item label="卡耗提成">
			<el-radio-group v-model="goods.khtdtype">
				<el-radio :label="1">百分比</el-radio>
				<el-radio :label="2">固定金额</el-radio>
			</el-radio-group>
			<el-input v-model="goods.khpercent" placeholder="请输入耗卡百分比提成(0 ~ 100)" v-if="goods.khtdtype==1"></el-input>
			<el-input v-model="goods.khfixed" placeholder="请输入耗卡固定金额提成" v-else></el-input>
		</el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submit">保存</el-button>
            <el-button @click="close">取消</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
import C from '#/src/common/common.js'
import Goods from '#/src/shop/goods.js'


export default {
    name: 'openAddShopGoods',
    data() {
        return {
            goods: {
                id: 0,
                name: '',
                price: '',
                categoryid: 0,
                brandid: 0,
                remarks: '',
                nums: 0,
                percent: '',
                fixed: '',
                tdtype: 1,
                khpercent: '',
                khfixed: '',
                khtdtype: 1,
                number: '',
            },
            bname: '',
            cname: ''
        }
    },
    methods: {
        selcate: async function() {
            let res = await Goods.openGoodsCategoryList()
            if (res) {
                this.goods.categoryid = res.id
                this.cname = res.name
            }
        },
        selbran: async function() {
            let res = await Goods.openBrandList()
            if (res) {
                this.goods.brandid = res.id
                this.bname = res.name
            }
        },
        async submit () {
            if (/^\s*$/.test(this.goods.name)) {
                C.error('产品名称不能为空！')
                return false
            } else if (this.goods.number === '') {
                C.error('产品编号不能为空！')
                return false
            } else if (!/^[1-9]+\d*(\.)?\d*$/.test(this.goods.price)) {
                C.error('请输入产品价格！')
                return false
            }

            if (this.goods.tdtype == 1) {
                if (!/^(\d|[1-9]\d|100)$/.test(this.goods.percent)) {
                    C.error('请输入产品销售提点（0 ~ 100）')
                    return false
                }
            } else {
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.goods.fixed)) {
                    C.error('请输入产品销售固定金额提成')
                    return false
                }
            }

            if (this.goods.khtdtype == 1) {
                if (!/^(\d|[1-9]\d|100)$/.test(this.goods.khpercent)) {
                    C.error('请输入产品卡耗提点（0 ~ 100）')
                    return false
                }
            } else {
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.goods.khfixed)) {
                    C.error('请输入产品卡耗固定金额提成')
                    return false
                }
            }

            if (this.goods.categoryid == 0) {
                C.error('请选择产品所属分类！')
                return false
            } else if (this.goods.brandid == 0) {
                C.error('请选择产品所属品牌！')
                return false
            }

            var ssum = 0
            var khsum = 0
            if (this.goods.tdtype === 1) {
                ssum = parseInt(this.goods.price) * parseInt(this.goods.percent) / 100
            } else {
                ssum = parseInt(this.goods.fixed)
            }

            if (this.goods.khtdtype === 1) {
                khsum = parseInt(this.goods.price) * parseInt(this.goods.khpercent) / 100
            } else {
                khsum = parseInt(this.goods.khfixed)
            }
            var allsum = parseInt(ssum + khsum)

            if (this.goods.price < allsum) {
                C.error('输入的提成超出项目价格')
                return false
            }

            let data = await C.ajax('/shop/goods/addedit', this.goods, 'post')

            if (data) {
                C.closeBrowserWindow(data)
            }
        },
        close () {
            C.closeBrowserWindow()
        }
    },
    mounted: function() {
        let row = C.getDatas()
        if (row && row.id) {
            this.goods.id = row.id
            this.goods.name = row.name
            this.goods.price = row.price
            this.goods.percent = Number(row.percent * 100).toFixed(0)
            this.goods.fixed = row.fixed
            this.goods.tdtype = row.tdtype
            this.goods.khpercent = Number(row.khpercent * 100).toFixed(0)
            this.goods.khfixed = row.khfixed
            this.goods.khtdtype = row.khtdtype
            this.goods.categoryid = row.categoryid
            this.goods.brandid = row.brandid
            this.goods.remarks = row.remarks
            this.goods.nums = row.nums
            this.goods.number = row.number
            this.bname = row.bname
            this.cname = row.cname
        }
    }
}
</script>